﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace zytech.common.baseform.sys
{
    public partial class MainFrame : Form
    {
        protected string name;
        protected string pwd;
        DataSet ds1;
        int state = 0;
        /// <summary>
        /// 数据库连接字符串，需在继承的窗体的构造函数中赋值
        /// </summary>
        protected string SqlConnStr = "";
        public MainFrame()
        {
            InitializeComponent();
        }
        public MainFrame(string n, string p)
        {
            InitializeComponent();
            this.name = n;
            this.pwd = p;
        }

        private void MainFrame_Load(object sender, EventArgs e)
        {
            /*
            if (name == "admin")        //管理员账号 则加载所有菜单
                SetAdminMenu();
            else
                SetOtherMenu(name);     //其他按照权限加载
            toolStripStatusLabel4.Text = "当前操作员:" + LoginForm.username + "        ";
            toolStripStatusLabel1.Text = "当前使用单位:金华正元计算机";
            toolStripStatusLabel3.Text = "当前时间:" + DateTime.Now.ToString("yyyy年m月d日 ") + "(" + zytech.common.Functions.ConvertDateTime(DateTime.Now) + ")";
            */
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void tileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void tileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void arrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void closeAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        protected virtual void ReLoginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Form fm = new Login();
            //fm.Show();
            //this.Hide();
        }

        protected virtual void PwdEditToolStripMenuItem_Click(object sender, EventArgs e)
        {
            /* Form fm = new PwdModify();
            fm.MdiParent = this;
            fm.Show();*/
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        protected virtual void AboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
             /*Form fm1 = new About();
             fm1.MdiParent = this;
             fm1.Show();*/
        }
        protected void ShowMdiWindow(Form Cfm,Form Pfm)
        {
            int IsOpen;
            IsOpen = 0;
            foreach (Form frm in Pfm.MdiChildren)
            {
                if (frm.Name == Cfm.Name)
                {
                    Cfm.Activate();
                    IsOpen = 1;
                    MessageBox.Show(Pfm.ActiveMdiChild.Name);
                }
            }
            if (IsOpen == 0)
            {
                Cfm.MdiParent = Pfm;
                Cfm.Show();
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabel3.Text = "当前时间:" + DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss") + "(" + Functions.ConvertDateTime(DateTime.Now) + ")";
        }

        protected virtual void SetAdminMenu()
        {
            string sql1 = "Select * from sys_menu";
            SqlConnection conn = new SqlConnection(SqlConnStr);
            SqlCommand com1 = new SqlCommand(sql1, conn);
            SqlDataAdapter da1 = new SqlDataAdapter(com1);
            ds1 = new DataSet();
            da1.Fill(ds1);

            foreach (DataRow row in ds1.Tables[0].Rows)
            {
                if (Convert.ToInt32(row["ParentId"]) == 0)
                {
                    ToolStripMenuItem tsmi = new ToolStripMenuItem(row["menu_name"].ToString());//权限名称
                    tsmi.Tag = row["menu_id"].ToString();//菜单编号
                    ms.Items.Add(tsmi);//将菜单项添加到菜单中
                    GetAdminChild(tsmi);//递归创建子菜单
                }
            }
        }
        protected virtual void SetOtherMenu(string str)
        {
            state = 0;
            string sql1 = "Select * from sys_menu";
            SqlConnection conn = new SqlConnection(SqlConnStr);
            SqlCommand com1 = new SqlCommand(sql1, conn);
            SqlDataAdapter da1 = new SqlDataAdapter(com1);
            ds1 = new DataSet();
            da1.Fill(ds1);
            foreach (DataRow row in ds1.Tables[0].Rows)
            {
                if (Convert.ToInt32(row["ParentId"]) == 0)
                {
                    string text1 = "select * from v_menu where cUserid = '" + str + "' and menu_id = '" + row["menu_id"] + "'";
                    SqlCommand com3 = new SqlCommand(text1, conn);
                    SqlDataAdapter da3 = new SqlDataAdapter(com3);
                    DataSet ds3 = new DataSet();
                    da3.Fill(ds3);
                    foreach (DataRow row1 in ds3.Tables[0].Rows)
                    {
                        if (Convert.ToInt32(row1["abled"]) == 1)
                            state = 1;
                    }
                    if (state == 1)
                    {
                        ToolStripMenuItem tsmi = new ToolStripMenuItem(row["menu_name"].ToString());//权限名称
                        tsmi.Tag = row["menu_id"].ToString();//菜单编号
                        ms.Items.Add(tsmi);//将菜单项添加到菜单中
                        GetOtherChild(tsmi);//递归创建子菜单
                        state = 0;
                    }
                }
            }
        }
        protected virtual void GetAdminChild(ToolStripMenuItem tsmi)
        {
            //遍历ds的所有的行
            foreach (DataRow row in ds1.Tables[0].Rows)
            {
                //如果ParentId 列等于当前菜单的ID，就将它添加到当前菜单下
                if (Convert.ToInt32(row["ParentId"]) == Convert.ToInt32(tsmi.Tag))
                {
                    ToolStripMenuItem tsm = new ToolStripMenuItem(row["menu_name"].ToString());
                    tsm.Tag = row["menu_id"].ToString();
                    //如果窗体名称列中不为空，表示此菜单下可以显示相应窗体
                    if (row["formName"].ToString() != "")
                    {
                        tsm.Click += new EventHandler(tsm_admin_Click);//订阅菜单单击事件
                    }
                    tsmi.DropDownItems.Add(tsm);
                    GetAdminChild(tsm);//递归创建子菜单
                }
            }
        }
        protected virtual void GetOtherChild(ToolStripMenuItem tsmi)
        {
            state = 0;
            SqlConnection conn = new SqlConnection(SqlConnStr);
            //遍历ds的所有的行
            foreach (DataRow row in ds1.Tables[0].Rows)
            {
                //如果ParentId 列等于当前菜单的ID，就将它添加到当前菜单下
                if (Convert.ToInt32(row["ParentId"]) == Convert.ToInt32(tsmi.Tag))
                {
                    string text2 = "select * from v_menu where cUserid = '" + name + "' and menu_id = '" + row["menu_id"] + "'";
                    SqlCommand com4 = new SqlCommand(text2, conn);
                    SqlDataAdapter da4 = new SqlDataAdapter(com4);
                    DataSet ds4 = new DataSet();
                    da4.Fill(ds4);
                    foreach (DataRow row2 in ds4.Tables[0].Rows)
                    {
                        if (Convert.ToInt32(row2["abled"]) == 1)
                            state = 1;
                    }
                    if (state == 1)
                    {
                        ToolStripMenuItem tsm = new ToolStripMenuItem(row["menu_name"].ToString());
                        tsm.Tag = row["menu_id"].ToString();
                        //如果窗体名称列中不为空，表示此菜单下可以显示相应窗体
                        if (row["formName"].ToString() != "")
                        {
                            tsm.Click += new EventHandler(tsm_other_Click);//订阅菜单单击事件
                        }
                        tsmi.DropDownItems.Add(tsm);
                        GetOtherChild(tsm);//递归创建子菜单
                    }
                    state = 0;

                }
            }
        }

        //点击节点加载其子节点（其他人员）
        protected virtual void tsm_other_Click(object sender, EventArgs e)
        {
            /*打开菜单对应的窗口，所以必需在继承的窗口中重写此功能
            //得到当前菜单项的Tag 属性，它表示菜单的ID
            DataRow[] rows = ds1.Tables[0].Select("menu_id=" + (sender as ToolStripMenuItem).Tag);
            //得到数据库中窗体的名称
            string formName = rows[0]["FormName"].ToString();//Formname为命名空间.窗体名
            string str = System.Windows.Forms.Application.StartupPath;
            //以下是通过反射动态实例化窗体对象
            Assembly assembly = Assembly.GetExecutingAssembly();
            Type t = assembly.GetType(formName);
            object obj = Activator.CreateInstance(t);
            Form frm = (Form)obj;
            frm.MdiParent = this;       //将窗体设置为main窗体的子窗体
            frm.Show();*/
        }
        //点击节点加载其子节点（管理员）
        protected virtual void tsm_admin_Click(object sender, EventArgs e)
        {
            /* 打开菜单对应的窗口，所以必需在继承的窗口中重写此功能
            //得到当前菜单项的Tag 属性，它表示菜单的ID
            DataRow[] rows = ds1.Tables[0].Select("menu_id=" + (sender as ToolStripMenuItem).Tag);
            //得到数据库中窗体的名称
            string formName = rows[0]["FormName"].ToString();//Formname为命名空间.窗体名
            string str = System.Windows.Forms.Application.StartupPath;
            //以下是通过反射动态实例化窗体对象
            Assembly assembly = Assembly.GetExecutingAssembly();
            Type t = assembly.GetType(formName);
            object obj = Activator.CreateInstance(t);
            Form frm = (Form)obj;
            frm.MdiParent = this;       //将窗体设置为main窗体的子窗体
            frm.Show();*/
        }

        private void MainFrame_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }        
    }
}
